import { _decorator, director, Label, Node } from 'cc'
import { SettingManager } from '../../data/SettingManager'
import { ClickBindingOptions, PropertyBindingOptions, ZController } from '../../Zcc'
const { ccclass } = _decorator

@ccclass('MenuController')
export class MenuController extends ZController {
	protected propertyBindings: Record<string, string | ({ path: string } & PropertyBindingOptions)> = {
		deceCountNode: 'top-area/left-top-widget/num/count/Label'
	}
	protected clickBindings: Record<string, string | ({ path: string } & ClickBindingOptions)> = {
		onSub: 'top-area/left-top-widget/num/count/sub',
		onAdd: 'top-area/left-top-widget/num/count/add',
		onShare: 'top-area/right-top-widget/share',
		onKf: 'top-area/right-top-widget/kf'
	}

	private deceCountNode: Node = null

	protected onControllerLoad(): void {
		console.log('MenuController onControllerLoad')
		this.deceCountNode.getComponent(Label).string = SettingManager.instance.settings.dice_num.toString()
	}

	private onSub() {
		let dice_num = SettingManager.instance.settings.dice_num
		if (dice_num >= 2) {
			dice_num--
			SettingManager.instance.updateSetting('dice_num', dice_num)
			this.deceCountNode.getComponent(Label).string = dice_num.toString()
			director.emit('onDiceCountChange')
		}
	}
	private onAdd() {
		let dice_num = SettingManager.instance.settings.dice_num
		if (dice_num <= 7) {
			dice_num++
			SettingManager.instance.updateSetting('dice_num', dice_num)
			this.deceCountNode.getComponent(Label).string = dice_num.toString()
			director.emit('onDiceCountChange')
		}
	}
	private onShare() {
		if (typeof wx !== 'undefined') {
			try {
				wx.shareAppMessage({
					title: '来摇骰子-今晚不养鱼'
				})
			} catch (e) {
				console.error('分享失败', e)
			}
		}
	}
	private onKf() {
		if (typeof wx !== 'undefined') {
			try {
				wx.openCustomerServiceConversation({
					success: res => {
						const { path, query } = res
						console.log(path)
						console.log(query)
					}
				})
			} catch (e) {
				console.error('分享失败', e)
			}
		}
	}

	private onShaker(state: string) {
		console.log('onShaker', state)
	}
}
